//
//  MyTakeFilmViewController.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2018/1/13.
//  Copyright © 2018年 Lwx. All rights reserved.
//

#import "MyTakeFilmViewController.h"
#import "MyTakeFilmCell.h"
#import "MyTakeFilmModel.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#import "NewFilmViewController.h"
@interface MyTakeFilmViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIButton * plusBtn;
@property (nonatomic, strong)MyTakeFilmModel * model;
@property (nonatomic, assign)int currentPage;
@property (nonatomic, strong)NSMutableArray * dataArr;
@property (nonatomic, strong)NSString * videoUrl;
@property (nonatomic, assign)int filmID;
@end

@implementation MyTakeFilmViewController(Networking)
- (void)getTakeFilmData{
    NSDictionary * dic = @{
                           @"mid":[UserModel intance].member_id,
                           @"token":[UserModel intance].token,
                           @"page":@(1),
                           @"row":@(6)
                           };
    WS(ws)
    [self showprogressHUD];
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/diary/myVideo") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self hiddenProgressHUD];
        if (error) {
            [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
        }else{
            if ([obj[@"code"] integerValue] == 100000) {
                if (ws.currentPage == 1) {
                    [ws.dataArr removeAllObjects];
                }
                ws.model = [MyTakeFilmModel yy_modelWithDictionary:obj];
                [ws.dataArr addObjectsFromArray:ws.model.data];
                [ws.tableView reloadData];
            }else{
                [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
            }
        }
    }];
}

- (void)travelInforLikeBtnClick{
    NSDictionary * dic = @{
                           @"did":@(self.filmID),
                           @"mid":[UserModel intance].member_id,
                           @"token":[UserModel intance].token,
                           @"type":@(2)
                           };
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/diary/toLike") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        if (error) {
            [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
        }else{
            if ([obj[@"code"] integerValue] == 100000){
                [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
            }else{
                [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
            }
        }
    }];
}

@end

@implementation MyTakeFilmViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的美拍";
    self.view.backgroundColor = getColor(bgColor);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.plusBtn];
    [self.view addSubview:self.tableView];
    
    self.currentPage = 1;
    self.dataArr = [NSMutableArray array];
    [self getTakeFilmData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - init
- (UIButton *)plusBtn{
    if (!_plusBtn) {
        _plusBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 42 / 2, 40 / 2)];
        [_plusBtn setBackgroundImage:[UIImage imageNamed:@"xcyj_xinjian"] forState:UIControlStateNormal];
        [_plusBtn addTarget:self action:@selector(addNewFilm:) forControlEvents:UIControlEventTouchUpInside];
    }return _plusBtn;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = getColor(@"e5e5e5");
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        WS(ws)
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            // 进入刷新状态后会自动调用这个block
            ws.currentPage++;
            [ws getTakeFilmData];
        }];

        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            ws.currentPage = 1;
            [ws getTakeFilmData];
        }] ;
        [self.tableView.mj_header beginRefreshing];
    }return _tableView;
}

#pragma mark - action
- (void)addNewFilm:(UIButton*)sender{
    NSLog(@"加号");
    NewFilmViewController * newFilmVC = [[NewFilmViewController alloc] init];
    [self.navigationController pushViewController:newFilmVC animated:YES];
}

#pragma mark - tableView的delegate和dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellID = @"cell";
    [self.tableView registerClass:[MyTakeFilmCell class] forCellReuseIdentifier:cellID];
    MyTakeFilmCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[MyTakeFilmCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.modelData = self.dataArr[indexPath.row];
    NSString * filmStr = cell.modelData.filmID;
    cell.likeBtnBlock = ^(){
        NSLog(@"喜欢");
        self.filmID = [filmStr intValue];
        [self travelInforLikeBtnClick];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 220 / WIDTH_5S_SCALE;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyTakeFilmCell * cell = (MyTakeFilmCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    self.videoUrl = cell.modelData.vic;
    NSURL *url = [NSURL URLWithString:self.videoUrl];
    AVPlayer *avPlayer= [AVPlayer playerWithURL:url];
    if (@available(iOS 10.0, *)) {
        avPlayer.automaticallyWaitsToMinimizeStalling = NO;
    }
    AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
    playerViewController.player = avPlayer;
    [[UIViewController getCurrentVC] presentViewController:playerViewController animated:YES completion:nil];
    [playerViewController.player play];
}

@end
